﻿using System.Collections.Generic;
using InfoboardAdapterC.Common;
using NKnife.IoC;

namespace InfoboardAdapterC.Connection
{
    public class DbConnectionViewModel
    {
        private readonly SqlServerDbConnection _ServerDb = DI.Get<SqlServerDbConnection>();
        private readonly IniFileConfig _IniFile = DI.Get<IniFileConfig>();
        private readonly ParamentConfig _ParamentConfig = DI.Get<ParamentConfig>();
        /// <summary>
        /// 测试数据库连接
        /// </summary>
        /// <param name="dbServerName"></param>
        /// <param name="dbUserName"></param>
        /// <param name="dbPassword"></param>
        /// <returns></returns>
        public bool TestConnection(string dbServerName, string dbUserName, string dbPassword)
        {
            var connection = _ServerDb.DatabaseConnectionString(dbServerName, dbUserName, dbPassword);
            return _ServerDb.SqlConnectionTest(connection);
        }

        /// <summary>
        /// 获取数据库连接
        /// </summary>
        /// <returns></returns>
        public string GetConnection()
        {
            LoadConfig(out string dbServerName, out string dbUserName,
                out string dbPassword, out string dbName);
            return _ServerDb.DatabaseConnectionString(dbServerName, dbUserName, dbPassword, dbName);
        }

        /// <summary>
        /// 保存数据库连接配置
        /// </summary>
        /// <param name="dbServerName"></param>
        /// <param name="dbUserName"></param>
        /// <param name="dbPassword"></param>
        /// <param name="dbName"></param>
        public void SaveConfig(string dbServerName, string dbUserName, string dbPassword,string dbName)
        {
            _IniFile.IniWriteValue(nameof(DbConnectionViewModel),nameof(dbServerName),dbServerName);
            _IniFile.IniWriteValue(nameof(DbConnectionViewModel), nameof(dbUserName), dbUserName);
            _IniFile.IniWriteValue(nameof(DbConnectionViewModel), nameof(dbPassword), dbPassword);
            _IniFile.IniWriteValue(nameof(DbConnectionViewModel), nameof(dbName), dbName);
            _ParamentConfig.SaveConnectio(GetConnection());
        }

        /// <summary>
        /// 读取数据库连接配置
        /// </summary>
        /// <param name="dbServerName"></param>
        /// <param name="dbUserName"></param>
        /// <param name="dbPassword"></param>
        /// <param name="dbName"></param>
        public void LoadConfig(out string dbServerName, out string dbUserName, out string dbPassword
            ,out string dbName)
        {
            dbServerName = _IniFile.IniReadValue(nameof(DbConnectionViewModel), nameof(dbServerName));
            dbUserName = _IniFile.IniReadValue(nameof(DbConnectionViewModel), nameof(dbUserName));
            dbPassword = _IniFile.IniReadValue(nameof(DbConnectionViewModel), nameof(dbPassword));
            dbName = _IniFile.IniReadValue(nameof(DbConnectionViewModel), nameof(dbName));
        }

        /// <summary>
        /// 获取所有在线数据库服务器名称
        /// </summary>
        /// <returns></returns>
        public List<string> LoadServerNames()
        {
            return _ServerDb.GetSqlServerNames();
        }
        /// <summary>
        /// 查询sql中的非系统库
        /// </summary>
        /// <param name="dbServerName">服务器名</param>
        /// <param name="dbUserName">用户名</param>
        /// <param name="dbPassword">密码</param>
        /// <returns></returns>
        public List<string> LoadDbNames(string dbServerName, string dbUserName, string dbPassword)
        {
            var connection = _ServerDb.DatabaseConnectionString(dbServerName, dbUserName, dbPassword);
            return _ServerDb.DatabaseList(connection);
        }
    }
}
